﻿using System;
using GoShopping.BLL;
using GoShopping.Model;
using GoShopping.BLL.PrdctInfo;

public partial class CommonProducts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["prdtId"] != null)
            {
                InitPage(Request.QueryString["prdtId"]);
                hiddenPrdtID.Value = Request.QueryString["prdtId"].ToString();
                AddPrdtToRecentList();
            }
            else
            {
                Response.Redirect("Error.aspx");
            }
        }
        if (Profile.IsAnonymous)
        {
            remarkLogin.Style.Add("display", "block");
            remarkDo.Style.Add("display", "none");
            //ClientScript.RegisterClientScriptBlock(GetType(), "remarkLogin", "<script type=text/javascript></script>");
        }
        else
        {
            remarkLogin.Style.Add("display", "none");
            remarkDo.Style.Add("display", "block");
        }
    }

    private void InitPage(string p)
    {
        //divRemark.Style.Add("display", "none");
        try
        {
            string imgSrc = "~" + GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(p), new ProductBean().GetProduct(Convert.ToInt32(p)).ProductTypeID)).Substring(1, GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(p), new ProductBean().GetProduct(Convert.ToInt32(p)).ProductTypeID)).Length - 1);
            Image1.ImageUrl = imgSrc;
            //string imgSrc = "~" + GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(Request.QueryString["prdtId"]), new GoShopping.BLL.ProductBean().GetProduct(Convert.ToInt32(Request.QueryString["prdtId"])).ProductTypeID)).Substring(1, GetWebPath(GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(Request.QueryString["prdtId"]), new GoShopping.BLL.ProductBean().GetProduct(Convert.ToInt32(Request.QueryString["prdtId"])).ProductTypeID)).Length - 1);
            //Image1.ImageUrl = imgSrc;
            Product pd = new GoShopping.BLL.ProductBean().GetProduct(Convert.ToInt32(p));
            lblPrdtName.Text = pd.Name;
            lblMarketPrice.Text = pd.PriceMarket.ToString();
            lblSalePrice.Text = pd.Price.ToString();
            lblCount.Text = pd.Count.ToString();
            lblAccessory.Text = pd.Accessory;

            GoShopping.BLL.SysInfo.RelatedPrdctBean rpb = new GoShopping.BLL.SysInfo.RelatedPrdctBean();
            //lvRelatedPrdts.DataSource = rpb.GetRelatedById(Convert.ToInt32(p));

            ltrBrandDescrip.Text = "";//No description
            ltrDescrip.Text = pd.BriefIntroduction;
            ltrSupplyer.Text = new GoShopping.BLL.ProductSuplyBean().GetProductSuply(pd.SupplyerID).Remark;

            lblUser.Text = Context.User.Identity.Name;
            GetPrdtRemarks(p);
        }
        catch (NullReferenceException nre)
        {
            GoShopping.Utility.MessageBox.Show(this, "此商品不存在!");
        }
        catch (Exception e)
        {
 
        }
    }
    string GetWebPath(string path)
    {
        return GoShopping.Utility.WebPath.ConvertSpecifiedPathToRelativePath(this, path).Replace("~", ".");
    }
    private void GetPrdtRemarks(string p)
    {
        rptRemark.DataSource = new GoShopping.BLL.SysInfo.RemarkPrdctBean().GetRemarkPrdctList(" AND ProductID=" + Convert.ToInt32(p));
        rptRemark.DataBind();
    }


    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        if (Profile.grpShoppingCart.MyCart.IsDuplicate(hiddenPrdtID.Value))
        {
            GoShopping.Utility.MessageBox.Show(this, "已经加入购物车!");
            return;
        }
        Profile.grpShoppingCart.MyCart.AddToCart(new CartItem(hiddenPrdtID.Value, lblPrdtName.Text, Convert.ToDecimal(lblSalePrice.Text), 1));
        Profile.Save();
        Response.Redirect("ShoppingCart.aspx");
    }

    protected void btnRemark_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Context.User.Identity.IsAuthenticated)
            {
                GoShopping.Utility.MessageBox.Show(this, "请您先登录!");
                return;
            }
            GoShopping.BLL.SysInfo.RemarkPrdctBean rpb = new GoShopping.BLL.SysInfo.RemarkPrdctBean();
            RemarkPrdct rp = new RemarkPrdct();
            rp.Content = txtRemarks.Text.Trim();
            rp.CreateDate = DateTime.Now;
            rp.UserID = new GoShopping.BLL.UserBean().GetOneUser(Context.User.Identity.Name).ID;
            rp.Title = txtTitle.Text.Trim();
            rp.ProductID = Convert.ToInt32(Request.QueryString["prdtId"]);
            rpb.AddOneRemarkPrdct(rp);
            GetPrdtRemarks(Request.QueryString["prdtId"]);
        }
        catch
        {
            Response.Redirect("Error.aspx");
        }
        //divRemark.Style.Add("display", "block");
        //ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('评论成功!')</script>", false);
    }
    protected void btnStockout_Click(object sender, EventArgs e)
    {
        if (!User.Identity.IsAuthenticated)
        {
            GoShopping.Utility.MessageBox.Show(this, "您还没有登录,请先登录!");
            return;
        }
        StockoutBean sob = new StockoutBean();
        Stockout so = new Stockout();
        so.UserID = new GoShopping.BLL.UserBean().GetOneUser(Context.User.Identity.Name).ID;
        so.Name = lblPrdtName.Text;
        so.CreateDate = DateTime.Now;
        sob.Add(so);
        GoShopping.Utility.MessageBox.Show(this, "登记成功!");
    }
    protected void btnFavourite_Click(object sender, EventArgs e)
    {
        if (!Context.User.Identity.IsAuthenticated)
        {
            GoShopping.Utility.MessageBox.Show(this, "请您先登录!");
            return;
        }
        if (Profile.grpFav.IsDuplicate(hiddenPrdtID.Value))
        {
            GoShopping.Utility.MessageBox.Show(this, "已经加入收藏夹!");
            return;
        }
        Profile.grpFav.AddToFav(new FavItem(hiddenPrdtID.Value, lblPrdtName.Text, (Image1.ImageUrl = Image1.ImageUrl == "" ? "暂时没有" : Image1.ImageUrl), Convert.ToDecimal(lblMarketPrice.Text)));
        Profile.Save();
    }

    private void AddPrdtToRecentList()
    {
        RecentPrdt rp = new RecentPrdt();
        rp.RecentId = Request.QueryString["prdtId"];
        rp.RecentName = lblPrdtName.Text;
        rp.RecentPrice = Convert.ToDecimal(lblSalePrice.Text);
        rp.Src = Image1.ImageUrl;
        Profile.grpRecent.AddRecent(rp);
        Profile.Save();
    }
    //protected void lvPrdts_ItemCommand(object sender, ListViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "Prdt")
    //    {
    //        AddPrdtToRecentList(e.CommandArgument);
    //        Response.Redirect("CommonProducts.aspx?prdtId=" + e.CommandArgument);
    //    }
    //}
    //protected void lvPrdts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    //{
    //    ((DataPager)(this.lvPrdts.FindControl("DataPager1"))).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    //    BindListView();
    //}
}
